Use case: conversion by polling

Description of usage

Operation

HAL1

HAL2

Start conversion

Activate the ADC peripheral and start the conversion using:

  • HAL_ADC_Start() for the regular group.

  • HAL_ADCEx_InjectedStart for the injected group.

Activate the ADC peripheral and start the conversion using HAL_ADC_Start().

Note

From this point, the ADC internal analog hardware is enabled, inducing a slight current consumption.

Start the conversion with a trigger using:

  • HAL_ADC_REG_StartConv() for the regular group.

  • HAL_ADC_INJ_StartConv() for the injected group.

Note

The conversion trigger can be:

  • A software start: calls the function for each conversion.

  • From another peripheral (timer, for instance): calls the function once.

Calibration (optional)

  • Start calibration using HAL_ADCEx_Calibration_Start().

  • Get calibration value using HAL_ADCEx_Calibration_GetValue().

  • Set calibration value using HAL_ADCEx_Calibration_SetValue().

  • Calibrate the ADC using HAL_ADC_Calibrate().

  • Get calibration factor using HAL_ADC_GetCalibrationFactor().

  • Set calibration factor using HAL_ADC_SetCalibrationFactor().

Wait for conversion completion

Wait for ADC conversion completion using:

  • HAL_ADC_PollForConversion() for the regular group.

  • HAL_ADCEx_InjectedPollForConversion() for the injected group.

Note

For specific events (such as ADC_EOSMP_EVENT or ADC_AWD1_EVENT), use HAL_ADC_PollForEvent().

Poll for conversion completion using:

  • HAL_ADC_REG_PollForConv() for the regular group.

  • HAL_ADC_INJ_PollForConv for the injected group.

Note

For specific events (such as ADC_EOSMP_EVENT or ADC_AWD1_EVENT), use HAL_ADC_PollForEvent().

Retrieve conversion results

Retrieve conversion results using:

  • HAL_ADC_GetValue() for the regular group.

  • HAL_ADCEx_InjectedGetValue for the injected group.

Retrieve conversion results using:

  • HAL_ADC_REG_GetValue() for the regular group.

  • HAL_ADC_INJ_GetValue for the injected group.

Stop conversion

Stop potential ongoing conversions and disable the ADC peripheral using:

  • HAL_ADC_Stop() for the regular and injected groups.

  • HAL_ADCEx_RegularStop for the regular group.

  • HAL_ADCEx_InjectedStop for the injected group.

If the conversion trigger is not a software start (from another peripheral, such as a timer), stop the conversion using:

  • HAL_ADC_REG_StopConv() for the regular group.

  • HAL_ADC_INJ_StopConv for the injected group.

Stop the ADC using HAL_ADC_Stop().

Loop for multiple iterations

N/A

Multiple iterations for starting and stopping conversions are possible.

Warning

If multiple conversions are required, do not fully stop the ADC using the HAL_ADC_Stop() function.

Sequence diagram

ADC conversion by polling

@startuml

participant "Application" as App
participant "ADC" as ADC

App -> ADC: <font color=green><b>HAL_ADC_Start() <font color=black>/ <font color=blue><b>HAL_ADCEx_InjectedStart()
App -> ADC: <font color=green><b>HAL_ADC_PollForConversion() <font color=black>/ <font color=blue><b>HAL_ADCEx_InjectedPollForConversion()
App -> ADC: <font color=green><b>HAL_ADC_GetValue() <font color=black>/ <font color=blue><b>HAL_ADCEx_InjectedGetValue()
App -> ADC: <font color=green><b>HAL_ADC_Stop() <font color=black>/ <font color=green><b>HAL_ADCEx_RegularStop() <font color=black>/ <font color=blue><b>HAL_ADCEx_InjectedStop()

@enduml

Code snippet

HAL1

HAL2

Regular group use case

while(1)
{
  /* Start the ADC conversion */
  HAL_ADC_Start(&hadc1);

  /* Poll for the end of conversion */
  if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
  {
      /* Read the ADC value */
      adcValue = HAL_ADC_GetValue(&hadc1);
  }

  /* Stop the ADC conversion */
  HAL_ADC_Stop(&hadc1);

}

Regular group use case

/* Start the ADC  */
HAL_ADC_Start(&hadc1);
while(1)
{
  /* Start the ADC conversion */
  HAL_ADC_REG_StartConv(&hadc1);

  /* Poll for the end of conversion */
  if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
  {
      /* Read the ADC value */
      adcValue = HAL_ADC_GetValue(&hadc1);
  }

  /* Stop the ADC regular conversion */
  HAL_ADC_REG_StopConv(&hadc1);

}
/* Stop the ADC */
HAL_ADC_Stop(&hadc1);

Injected group use case

while(1)
{
  /* Start the ADC injected conversion */
  HAL_ADCEx_InjectedStart(&hadc1);

  /* Poll for the end of injected conversion */
  if (HAL_ADCEx_InjectedPollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
  {
      /* Read the ADC injected values */
      injectedValue1 = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_1);
      injectedValue2 = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_2);
  }

  /* Stop the ADC injected conversion */
  HAL_ADCEx_InjectedStop(&hadc1);

}

Injected group use case

/* Start the ADC  */
HAL_ADC_Start(&hadc1);

while(1)
{
  /* Start the ADC conversion */
  HAL_ADC_INJ_StartConv(&hadc1);

  /* Poll for the end of injected conversion */
  if (HAL_ADC_INJ_PollForConv(&hadc1, HAL_MAX_DELAY) == HAL_OK)
  {
      /* Read the ADC injected values */
      injectedValue1 = HAL_ADC_INJ_GetValue(&hadc1, 1);
      injectedValue2 = HAL_ADC_INJ_GetValue(&hadc1, 2);
  }

  /* Stop the ADC injected conversion */
  HAL_ADC_INJ_StopConv(&hadc1);

}
/* Stop the ADC  */
HAL_ADC_Stop(&hadc1);

Key changes

  1. The split of the activate and start conversion functions.

  2. The split of the disable and stop conversion functions.